package game.entity.actor;


import edu.monash.fit2099.engine.*;
import game.entity.action.*;
import game.entity.behavior.*;
import game.entity.item.Fruit;
import game.util.RandomUtil;

import java.util.List;

/**
 * A herbivorous dinosaur. 剑龙
 */
public class BabyStegosaur extends Stegosaur {
    // Will need to change this to a collection if Stegosaur gets additional Behaviours.
    private Class<Stegosaur> stegosaurClass;
    private int age;
    private final int GROW_TURN = 2;

    public BabyStegosaur() {
        super("baby Stegosaur", "male", 10);
        if (RandomUtil.shotOn(50)) {
            sex = "female";
        } else {
            sex = "male";
        }
    }

    @Override
    public Actions getAllowableActions(Actor otherActor, String direction, GameMap map) {
        Actions actions = new Actions();
//        饥饿，且player有水果，可被喂食
        if (behaviour instanceof HungerBehaviour || behaviour instanceof SleepBehaviour) {
            List<Item> inventory = otherActor.getInventory();
            for (Item item : inventory) {
                if (item instanceof Fruit) {
                    actions.add(new FeedAction(this, item));
                }
            }

        }
        return actions;
    }

    /**
     * 给你一系列可能的事件，每个角色根据behavior选择出下一步要触发的事件
     * Figure out what to do next.
     * <p>
     * FIXME: Stegosaur wanders around at random, or if no suitable MoveActions are available, it
     * just stands there.  That's boring.
     *
     * @see Actor#playTurn(Actions, Action, GameMap, Display)
     */
    @Override
    public Action playTurn(Actions actions, Action lastAction, GameMap map, Display display) {
        if (age == GROW_TURN) {
           return new GrowAction(new Stegosaur("Stegosaur", this.getSex()));
        }

//        昏迷状态,每回合计次； 持续20回合时，删除该角色
        if (behaviour instanceof SleepBehaviour) {
            if (sleepTurn == DEAD_TURN) {
                behaviour = new DeathBehaviour(this);
            } else {
                sleepTurn += 1;
                return behaviour.getAction(this, map);
            }
        }

//        周围事件如果满足触发条件，则直接触发
        for (Action action : actions) {
//           周围存在食物
            if (hitPoints < HUNGRY_POINTS && action instanceof EatAction) {
                return action;
            }
        }

//        周围事件不满足，再执行自己的事件
        if (hitPoints == 0) {//            无意识状态
            behaviour = new SleepBehaviour(this);
        } else if (0 < hitPoints && hitPoints < HUNGRY_POINTS) {
            //饿的时候 一定会去找吃的吧？
            behaviour = new HungerBehaviour(this);
        } else {
            behaviour = new WanderBehaviour();
        }
        hitPoints--;
        age++;
        return behaviour.getAction(this, map);
    }
}
